package wechat.user;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.ResultData;
import com.filter.BaseController;

import beans.subject.subsetquestion;
import beans.user.recommendgold;
import beans.user.user;
import beans.user.withdrawcash;
import service.user.RecommendgoldServiceImpl;
import service.user.WXUserServiceImpl;
import service.user.WithdrawcashServiceImpl;
@Controller
@RequestMapping("/wx")
public class WXUserapplyController extends BaseController{
	@Resource
	private WithdrawcashServiceImpl<withdrawcash> withdrawcashServiceImpl;

	@Resource
	private WXUserServiceImpl<user> wxUserServiceImpl;

	@Resource
	private RecommendgoldServiceImpl<recommendgold> recommendgoldServiceImpl;
	/**
	 * 提现申请
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/apply")
	public String apply() throws Exception {
		user u = (user)getReq().getSession().getAttribute("wxuser");
		user current = wxUserServiceImpl.queryById(u.getOpenid());
		getReq().setAttribute("current", current);
		return "wechat/usercenter/apply";
	}

	@ResponseBody
	@RequestMapping("/saveapply")
	public void saveapply(withdrawcash model) throws Exception {
		ResultData<String> rd = new ResultData<>();

		user u = (user)getReq().getSession().getAttribute("wxuser");
		user current = wxUserServiceImpl.queryById(u.getOpenid());
		if(Double.doubleToLongBits(model.getMoney())<Double.doubleToLongBits(0)) {
			rd.setType(3);
			rd.setMessage("金额格式不正确!");
			responseJson(rd);
			return;
		}
		if(Double.doubleToLongBits(model.getMoney())<Double.doubleToLongBits(100)) {
			rd.setType(3);
			rd.setMessage("提现金额必须大于等于100!");
			responseJson(rd);
			return;
		}
		if(current.getCurrentmoney()<model.getMoney()) {
			rd.setType(3);
			rd.setMessage("可提现金额不足!");
			responseJson(rd);
			return;
		}
		model.setAddtime(new Date());
		model.setId(UUID.randomUUID().toString());
		model.setUid(u.getOpenid());
		model.setStatus("1");
		withdrawcashServiceImpl.add(model);

		current.setCurrentmoney(current.getCurrentmoney()-model.getMoney());
		wxUserServiceImpl.updateCurrentMoney(current.getOpenid(),current.getCurrentmoney());

		rd.setType(1);
		rd.setMessage("提现申请成功!");
		responseJson(rd);
	}

	/**
	 * 收益信息
	 */
	@RequestMapping("/incommeonmoney")
	public String incommeonmoney() {
		return "wechat/usercenter/incommeonmoney";
	}


	@ResponseBody
	@RequestMapping("/incommeonmoneydata")
	public void incommeonmoneydata() {
		user u = (user)getReq().getSession().getAttribute("wxuser");
		List<recommendgold> goldList = recommendgoldServiceImpl.getByincommeId(u.getOpenid());

		if(goldList.size()>0) {
			String[] ids = new String[goldList.size()];
			int i=0;
			for(recommendgold gold : goldList) {
				ids[i] = gold.getOrderuid();
				i++;
			}
			List<user> uList = wxUserServiceImpl.queryByIds(ids);
			for(recommendgold gold : goldList) {
				for(user tem : uList) {
					if(gold.getOrderuid().equals(tem.getOpenid())) {
						gold.setForwhat(tem.getNickname()+"下单");
						break;
					}
				}
			}
		}
		responseJsonArray(goldList);
	}
}
